package com.springboot.template.thread.synchronizedDemo;

class Seat extends Thread {
    private static int position = 100;
    private static Object object = new Object();

//    @Override
//    public void run() {
//        synchronized (object) {
//            while (true) {
//                if (position > 0) {
//                    try {
//                        Thread.sleep(200);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                    System.out.println("从" + this.getName() + "得到座位，座位号为：" + position);
//                    position--;
//                } else {
//                    break;
//                }
//            }
//        }
//    }


    @Override
    public void run() {
        while (true){
            int position1 = getPosition();
            if (position1 <=0) {
                break;
            }
        }
    }

    private static synchronized int getPosition(){
        if (position > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("从" + Thread.currentThread().getName() + "得到座位，座位号为：" + position);
            position--;
        }
        return position;
    }
}

public class TestThread {
    public static void main(String[] args) {
        Seat seat = new Seat();
        Seat seat1 = new Seat();
        Seat seat2 = new Seat();

        seat.setName("渠道1");
        seat1.setName("渠道2");
        seat2.setName("渠道3");

        seat.start();
        seat1.start();
        seat2.start();
    }
}
